#include "RTC.h"

RTC_date_struct RTC_date;										//Struct for storing the date received from host
RTC_date_struct RTC_sut;										//Struct for storing the StartUpTime received from host

extern Com_Message_struct Com_Message_Rx;

/*
Call RTC_getDate to synchronize time with host
Init Hardware for RTC
*/

void RTC_init(void)
{
	RTC_getDate();
}

/*
Request date from Host
Called once at initialization or every second until at least one valid message with ID_GET_DATE is received
*/


void RTC_getDate(void)
{
//	Com_Message_Tx_create(MC_ID_REQ_DATE, 0, 0);
//	Com_Message_Tx_send();
}

/*
Process message data, which was received from host
This procedure is called, if one message with ID_GET_DATE is received
Processed data will be written in RTC_date_struct RTC_date
*/

void RTC_processRxData(void)
{
	RTC_date.year   = Com_Message_Rx.data[0];
	RTC_date.month  = Com_Message_Rx.data[1];
	RTC_date.day    = Com_Message_Rx.data[2];
	RTC_date.hour   = Com_Message_Rx.data[3];
	RTC_date.minute = Com_Message_Rx.data[4];
	RTC_date.second = Com_Message_Rx.data[5];
	RTC_date.valid_flag = 1;
}

/*
Process message data, which was received from host
This procedure is called, if one message with ID_GET_SUT_DATE is received
Processed data will be written in RTC_date_struct RTC_sut
*/

void RTC_processSutData(void)
{
	RTC_sut.year   = Com_Message_Rx.data[0];
	RTC_sut.month  = Com_Message_Rx.data[1];
	RTC_sut.day    = Com_Message_Rx.data[2];
	RTC_sut.hour   = Com_Message_Rx.data[3];
	RTC_sut.minute = Com_Message_Rx.data[4];
	RTC_sut.second = Com_Message_Rx.data[5];
	RTC_sut.valid_flag = 1;
}

/*
Check every second if StartUpTime is reached
This procedure should only be called by 1000ms polling loop
*/

void RTC_checkSut(void)
{
	if(	(RTC_sut.valid_flag  == 1		  )	&&
		(RTC_date.valid_flag == 1		  ) &&
		(RTC_sut.year   == RTC_date.year  ) &&
		(RTC_sut.month  == RTC_date.month ) &&
		(RTC_sut.day    == RTC_date.day   ) &&
		(RTC_sut.hour   == RTC_date.hour  ) &&
		(RTC_sut.minute == RTC_date.minute) &&
		(RTC_sut.second == RTC_date.second))
	{
		LED_TOGGLE(LED1);											//Place code for StartUp here
		RTC_sut.valid_flag = 0;
	}
}

/*
If valid_flag is not set, request time from host
Update Clock every second if valid_flag is set
This procedure should only be called by asynchronous interrupt, which is triggered by external crystal (32768 Hz)
*/

void RTC_updateClock(void)
{
	uint8_t day = 0;

	if(RTC_date.valid_flag)
	{
		RTC_date.second++;

		if(RTC_date.second == 60)
		{
			RTC_date.minute++;
			RTC_date.second = 0;
		}
		if(RTC_date.minute == 60)
		{
			RTC_date.hour++;
			RTC_date.minute = 0;
		}
		if(RTC_date.hour == 24)
		{
			RTC_date.day++;
			RTC_date.hour = 0;
		}

		day = RTC_date.day;
		switch(RTC_date.month)
		{
			case 1:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 2:
				if(day == 28)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 3:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 4:
				if(day == 30)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 5:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 6:
				if(day == 30)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 7:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 8:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 9:
				if(day == 30)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 10:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 11:
				if(day == 30)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			case 12:
				if(day == 31)
				{
					RTC_date.day = 0;
					RTC_date.month++;
				}
				break;
			default:
				break;
		}
		if(RTC_date.month == 12)
		{
			RTC_date.year++;
			RTC_date.month = 1;
		}
	}
	else
	{
		RTC_getDate();
	}
}
